package view.hero
{
import core.Language;

import flash.display.MovieClip;

import framework.core.TipManager;
import framework.proxy.ComponentProxy;
import framework.view.components.button.ButtonProxy;

import values.avatar.AvatarEquipPositionValue;
import values.avatar.IAvatarValue;
import values.events.AvatarValueEvent;
import values.item.EquipmentItemValue;

import view.component.asset.IconProxy;
import view.inventory.EquipmentSelectionUI;
import view.inventory.InventoryItemUtil;
import view.inventory.menu.HeroEquipmentMenu;

/**
 * HeroEquipmentItemRenderProxy
 * @author jliang
 *
 */
public class HeroEquipmentItemRenderProxy extends ComponentProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function HeroEquipmentItemRenderProxy()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	public function get icon():IconProxy
	{
		return proxys["icon"];
	}
	
	private var _data:EquipmentItemValue;

	public function get data():EquipmentItemValue
	{
		return _data;
	}

	public function set data(value:EquipmentItemValue):void
	{
		if (icon)
		{
			icon.stopAndRemove();
		}
		
		_data = value;
		
		if (!data) return;
		
		icon.load(data.template.getIconPathByKey("icon"));
	}
	
	private var _position:AvatarEquipPositionValue;

	public function get position():AvatarEquipPositionValue
	{
		return _position;
	}

	public function set position(value:AvatarEquipPositionValue):void
	{
		_position = value;
	}
	
	private var _avatar:IAvatarValue;

	public function get avatar():IAvatarValue
	{
		return _avatar;
	}

	public function set avatar(value:IAvatarValue):void
	{
		_avatar = value;
		
		avatar.addEventListener(AvatarValueEvent.REMOVE_EQUIPMENT, removeEquipmentHandler, false, 5);
		avatar.addEventListener(AvatarValueEvent.ADD_EQUIPMENT, addEquipmentHandler, false, 5);
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function reset():void
	{
		if (icon)
		{
			icon.stopAndRemove();
		}
		
		if (avatar)
		{
			_data = null;
		}
		
		if (avatar)
		{
			avatar.removeEventListener(AvatarValueEvent.REMOVE_EQUIPMENT, removeEquipmentHandler);
			avatar.removeEventListener(AvatarValueEvent.ADD_EQUIPMENT, addEquipmentHandler);
			_avatar = null;
		}
	}
	//==========================================================================
	//  Overridden methods: RepeaterRenderProxy
	//==========================================================================	
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"", classRef: ButtonProxy, id:"btn", useCapture:true, onClick: onClickHandler, onMouseOver: onMouseOverHandler },
			{ skin:"", classRef: IconProxy, id:"icon" }
		];
	}
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
	}
	
	override public function dispose():void
	{
		_avatar = null;
		_data = null;
		_position = null;
		super.dispose();
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================	
	protected function onMouseOverHandler():void
	{
		if (data) 
		{
			InventoryItemUtil.showTip(skin, data);
		}
		else
		{
			TipManager.showTip(skin, position.name);
		}
	}
	
	protected function onClickHandler():void
	{
		if (data)
		{
			InventoryItemUtil.showMenu(skin, data, { skin:"swf.ui.HeroEquipmentMenuSkin", classRef:HeroEquipmentMenu });
		}
		else
		{
			EquipmentSelectionUI.showEquipmentByPosition(avatar, position.key);
		}
	}
	
	protected function removeEquipmentHandler(event:AvatarValueEvent):void
	{
		if (data == event.params.equipment)
		{
			if (icon)
			{
				icon.stopAndRemove();
			}
			
			if (data)
			{
				_data = null;
			}
		}
	}
	
	protected function addEquipmentHandler(event:AvatarValueEvent):void
	{
		var item:EquipmentItemValue = event.params.equipment;
		
		if (data == item)
		{
			return;
		}
		
		if (position.key == item.position)
		{
			data = item;
		}
	}
}
}



